home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 70 < prev    next >
Text File  |  1996-08-06  |  3KB  |  68 lines

  1. Newsgroups: comp.std.c
  2. Path: phcoms4.seri.philips.nl!panther!baynes
  3. From: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
  4. Subject: Re: Undefined result vs. int's holding undefined values.
  5. Sender: news@ukpsshp1.serigate.philips.nl (account for localnews)
  6. Message-ID: <DKyIx1.1qr@ukpsshp1.serigate.philips.nl>
  7. Date: Wed, 10 Jan 1996 08:43:49 GMT
  8. References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net> <4cs460$d6e@news.informix.com>
  9. Organization: Philips Semiconductors, Southampton, UK
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Daniel Wood (dwood@informix.com) wrote:
  13. : thads@csn.net (Thad Smith) wrote:
  14. : >In article <4cmg0s$1mb@der.twinsun.com>,
  15. : Do any machines exist which actually explode when you add two number together
  16. : such that the result would exceed MAXINT? :-)  Get pratical!
  17.  
  18. VAXes
  19.  
  20. If you set the apropriate bit in the process status word then integer overflow
  21. causes a trap. I think that by default this is not enabled, and it could be
  22. difficult to use in practice (it traps on signed overflow, but how do you 
  23. distinguish this from valid unsigned arithmetic? The compiler would have
  24. to generate instructions to switch this on and off. I can't recall if VAX
  25. C compiler offers this option, however I recall it is the default for VAX
  26. pascal to enable overflow checks.
  27.  
  28. It is true most compilers don't offer traping overflows and those that do
  29. tend not to do it by default. I think that this is partly because there is so 
  30. much C code out there that erroneously overflows but manages to keep working 
  31. that most people don't bother with it. However not bothering with it means
  32. that more accidental overflows are written into the code. Also it can add
  33. alot of execution overhead, even when there is hardware support there may
  34. be some.
  35.  
  36.  
  37.  
  38. The question started up over a bit of code that was written to check if
  39. overflow would occur. It has always seemed a weekness of C that there is
  40. no defined way to do this. As in the example interpretors are one case. However
  41. there are may applications where it would be better to crash than produce
  42. wrong answeres that will cause trouble latter. Even better would be a simple
  43. way for applications to check if certain arithmetic operations have worked
  44. safely. Can anyone make a recomendation for a good language extension to add 
  45. this? Because there is so much legacy code out there it must be selective and
  46. must not add overhead to unchecked operations. It may be one needs three levels
  47. of operation:
  48.     1: Expected to overflow during normal operation. Overflow is safe and
  49.         should not be reported. [This is the backwards compatible
  50.         case for unsigned and may need to be for signed.]
  51.     2: Overflow is an error, if practicable it should be caught and reported
  52.         by some implementation specified means. [This is the strict backwards
  53.         compatible case for signed. It probably should be the default for
  54.         most code.]
  55.     3: Overflow is possible. It _must_ be caught and handled by the application.
  56.         [For example it could set errno or raise a signal - but it must be
  57.            defined.]
  58.  
  59. How do we handle these cases in standard C? What changes can we make to the
  60. standard to make it possible?
  61.  
  62.  
  63. --
  64. Stephen Baynes                              baynes@mulsoc2.serigate.philips.nl
  65. Philips Semiconductors Ltd
  66. Southampton                                 My views are my own.
  67. United Kingdom
  68.